﻿define([
    'Wjs/data/Loader',
    'Wjs/data/HttpRequest'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'RemoteLoader',

        internal: {

            base: Wjs.getType('Loader')
        },

        onResponse: function (fcn) {

            this.responseHandler = fcn;

            return this;
        },

        setHttpRequest: function (httpRequest) {
            var me = this;

            httpRequest
                .onSuccess(function (response, callback) {
                    var data;

                    if (!me.contentType) {

                        me.contentType = me.getContentTypeFromHeader(response.headers);
                    }
                    
                    switch (me.contentType) {
                        case 'json':
                            {
                                data = JSON.parse(response.text);
                            }
                            break;
                        case 'xml':
                            {
                                data = response.xml;
                            }
                            break;
                        default:
                            {
                                data = response.text;
                            }
                            break;
                    }

                    if (me.responseHandler && // Handle the response regardless if there is data or not

                        me.responseHandler.call(me.scope, data) === false) {

                        return; // Returning false prevents from executing the other handlers
                    }

                    me.handleDataEvents(data, callback);
                })
                .onComplete(function () {

                    if (me.scope && me.scope.mask) {

                        me.scope.mask.hide();
                    }
                });

            me.httpRequest = httpRequest;

            return me;
        },

        getHttpRequest: function() {

            return this.httpRequest;
        },

        // Retrieve the content type from the content header returned from the server
        getContentTypeFromHeader: function(headers) {
            var contentHeader = headers['Content-Type'];

            if (contentHeader.indexOf('/html') > -1) {

                return 'html';
            }
            else if (contentHeader.indexOf('/json') > -1 ||
                    contentHeader.indexOf('/javascript') > -1) { // 

                return 'json';
            }
            else if (contentHeader.indexOf('/xml') > -1) {

                return 'xml';
            }
            else {

                return 'text';
            }
        },

        getData: function (callback) {
            var me = this;

            if (me.scope && me.scope.mask) {

                me.scope.mask.show({
                    loadingIcon: true
                });
            }

            me.httpRequest.send(callback);
        }

    });

});